<template>
  <div>
    <!-- 面包导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/users' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
      <el-breadcrumb-item>用户列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片区域 -->
    <el-card class="box-card">
      <!-- 搜索区域 -->
      <el-row :gutter="20">
        <el-col :span="10">
          <!-- 输入框部分 -->
          <!-- clearable表示可清空 -->
          <!-- clear表示点击重置 -->
          <el-input placeholder="请输入内容" clearable v-model="queryInfo.query" @clear="getUserList">
            <el-button slot="append" icon="el-icon-search" @click="getUserList"></el-button>
          </el-input>
        </el-col>
        <el-col :span="2">
          <!-- 按钮部分 -->
          <el-button type="primary" plain @click="addDialogVisable=true">添加用户</el-button>
        </el-col>
      </el-row>

      <!-- 用户列表数据 -->
      <el-table :data="userList" border stripe header-align="center">
        <!-- 给列添加type="index"就是加一个索引 -->
        <el-table-column type="index"></el-table-column>
        <el-table-column align="center" prop="username" label="姓名"></el-table-column>
        <el-table-column align="center" prop="email" label="邮箱"></el-table-column>
        <el-table-column align="center" prop="mobile" label="电话"></el-table-column>
        <el-table-column align="center" prop="role_name" label="角色"></el-table-column>
        <el-table-column align="center" label="状态">
          <!-- 运用的是作用域插槽 -->
          <template slot-scope="scope">
            <!-- scope.row 的这个row是element底层封装好的 主要作用是：获取当前行的数据-->
            <el-switch v-model="scope.row.mg_state" @change="userStateChange(scope.row)"></el-switch>
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作">
          <template>
            <el-button plain size="mini" type="primary" icon="el-icon-edit"></el-button>
            <el-button plain size="mini" type="danger" icon="el-icon-delete"></el-button>
            <el-tooltip effect="dark" content="分配权限" placement="top">
              <el-button plain size="mini" type="warning" icon="el-icon-setting"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[1, 2, 5, 10]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>

    <!-- 添加用户弹框 -->
    <el-dialog title="添加用户" :visible.sync="addDialogVisable" width="30%">
      <!-- 弹框的主体区域 -->
      <el-form :model="addForm" :rules="addFormRules" ref="addFormRef" label-width="70px">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="addForm.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="addForm.password"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="addForm.email"></el-input>
        </el-form-item>
        <el-form-item label="手机" prop="mobile">
          <el-input v-model="addForm.mobile"></el-input>
        </el-form-item>
      </el-form>

      <!-- 弹框的底部区域 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisable = false">取 消</el-button>
        <el-button type="primary" @click="addDialogVisable = false">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>s

<script>
export default {
  name: 'User',
  data() {
    return {
      // 获取用户列表参数
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 5
      },
      // 存储用户信息的
      userList: [],
      // 用户总条目数
      total: 0,
      addDialogVisable: false,
      // 添加弹窗用户数据
      addForm: {
        username: '',
        password: '',
        email: '',
        mobile: ''
      },
      // 添加用户数据的验证
      addFormRules: {
        // 对用户名进行验证
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 6, message: '长度在 3 到 6 个字符', trigger: 'blur' }
        ],
        // 对密码进行验证
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 5, max: 15, message: '请输入正确的密码', trigger: 'blur' }
        ],
        // 对邮箱进行验证
        email: [{ required: true, message: '请输入邮箱地址', trigger: 'blur' }],
        // 对手机号进行验证
        mobile: [{ required: true, message: '请输入手机号', trigger: 'blur' }]
      }
    }
  },
  created() {
    // 调用获取用户列表数据的方法
    this.getUserList()
  },
  methods: {
    // 当用户状态发生改变的时候触发的事件
    async userStateChange(userList) {
      const { data: res } = await this.$http.put(`/users/${userList.id}/state/${userList.mg_state}`)
      if (res.meta.status !== 200) {
        userList.mg_state = !userList.mg_state
        return this.$message.error(res.meta.msg)
      }
      this.$message.success(res.meta.msg)
    },
    // 当pagenum改变时触发的事件
    handleCurrentChange(pagenum) {
      this.queryInfo.pagenum = pagenum
      this.getUserList()
    },
    // 当pagesize改变时侯触发的事件
    handleSizeChange(pagesize) {
      this.queryInfo.pagesize = pagesize
      this.getUserList()
    },
    // 获取用户数据
    async getUserList() {
      const { data: res } = await this.$http.get('/users', {
        params: this.queryInfo
      })
      if (res.meta.status !== 200) {
        return this.$message.error(res.meta.msg)
      }
      this.userList = res.data.users
      this.total = res.data.total
    }
  }
}
</script>

<style lang="less" scoped>
</style>
